package com.bloveoacom.weichi.activity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bloveoacom.base.AppContext;
import com.bloveoacom.base.BaseList2Activity;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.exception.ApiException;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.HttpResult;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseListSubscriber2;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.AESCipher;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.DataUtil;
import com.bloveoacom.util.DialogUtil;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.util.PicCameraLocalUtil;
import com.bloveoacom.util.ShareUtils;
import com.bloveoacom.util.ToastUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.CreateEosWalletBean;
import com.bloveoacom.weichi.bean.MyFansBean;
import com.bloveoacom.weichi.bean.NewsPostBean;
import com.bloveoacom.weichi.bean.SimpleBean;
import com.bloveoacom.weight.InputDialog;
import com.bloveoacom.weight.LlkjToolBar;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.reactivestreams.Publisher;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.bloveoacom.http.HttpMethods.PAGE_START_SIZE;
import static com.bloveoacom.util.AESCipher.KEY;
import static com.bloveoacom.util.PicCameraLocalUtil.recycle;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/7/12
 * desc  : 我的关注或者粉丝
 */
public class CheckActivity extends BaseList2Activity<NewsPostBean> {

    @BindView(R.id.title_bar)
    LlkjToolBar titleBar;
    @BindView(R.id.tv_post_id)
    TextView tvPostId;
    @BindView(R.id.tv_post_title)
    TextView tvPostTitle;
    @BindView(R.id.layout_share)
    ConstraintLayout layoutShare;
    @Override
    public int getLayoutResId() {
        return R.layout.layout_refresh_title_share;
    }

    @Override
    public void initView() {
        super.initView();
        titleBar.setToolBar("评审",
                true, "", R.drawable.ic_back, true, "", R.drawable.ic_search);
    }


    @Override
    protected View getViewSetTB() {
        return titleBar;
    }

    @Override
    public void getBundleExtras(Bundle extras) {

    }



    @Override
    protected int getItemLayout() {
        return R.layout.rv_item_post;
    }

    @Override
    protected void convertView(BaseViewHolder helper, NewsPostBean item) {
        ProgressBar view1 = helper.getView(R.id.id_seekbar_1);
        ProgressBar view2 = helper.getView(R.id.id_seekbar_2);
       DataUtil.setItemView2(mContext,helper,item,4);
        if("1".equals(item.isOver)){
            helper.setVisible(R.id.group2,false);
            helper.setVisible(R.id.group,true);
            helper.setText(R.id.tv_check,"已完成");
            helper.setBackgroundColor(R.id.tv_check, ContextCompat.getColor(mContext,R.color.color_858585));
            view1.setProgress(0);
            view2.setProgress(0);
            helper.setText(R.id.tv_no_agreen,"0/21");
            helper.setText(R.id.tv_support,"0/80");
        }else{
            helper.setVisible(R.id.group2,true);
            helper.setVisible(R.id.group,false);
            if("1".equals(String.valueOf(item.isVote))){
                helper.setText(R.id.tv_check,"已评审");
                helper.setBackgroundColor(R.id.tv_check, ContextCompat.getColor(mContext,R.color.color_858585));
            }else{
                helper.setText(R.id.tv_check,"待评审");
                helper.setBackgroundColor(R.id.tv_check, ContextCompat.getColor(mContext,R.color.black));
            }
            view1.setProgress(item.supportNum);
            view2.setProgress(item.againstNum);
            helper.setText(R.id.tv_no_agreen,item.againstNum+"/21");
            helper.setText(R.id.tv_support,item.supportNum+"/80");
            helper.addOnClickListener(R.id.iv_share);
        }
    }

    @Override
    protected void getData() {
            BaseListSubscriber2<List<NewsPostBean>> baseSubscriber = HttpMethods.getInstance().getHttpService().queryUserVoteList(UserInfoBean.getInstance().getToken(), page, getPageSize()).compose(RxHelper.getData()).subscribeWith(new BaseListSubscriber2<List<NewsPostBean>>(this,page==PAGE_START_SIZE,page) {
                @Override
                public void onNext(List<NewsPostBean> o) {
                    doData(o);
                }
            });
            addDispose(baseSubscriber);
    }


    NewsPostBean bean;
    @Override
    public void initListener() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                bean = datas.get(position);
                ActivityUtil.startActivity(mContext,PostDetail2Activity.class,bean.coinId,String.valueOf(bean.isVote));
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                NewsPostBean newsPostBean = datas.get(position);
                tvPostId.setText("文章ID:"+newsPostBean.coinId);
                tvPostTitle.setText(newsPostBean.getTitle());
                share();
            }
        });
        titleBar.setRightButtonOnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.startActivity(mContext,SearchActivity.class);
            }
        });

    }
    Dialog dialog;
    Bitmap bitmap;
    File sharefile;
    private void share() {
        AppContext.mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                bitmap = PicCameraLocalUtil.getBitmap(layoutShare);
                sharefile = PicCameraLocalUtil.saveBitmap(mContext, bitmap, "dsh_post2");
                dialog = DialogUtil.getPicBottomDialog2(mContext, new DialogUtil.PhotoDoWhat() {
                    @Override
                    public void doone() {
                        ShareUtils.shareFriends(sharefile.getPath(), 0);
                        DialogUtil.dissDialog(dialog);
                        ToastUtil.makeShortText(mContext, "分享操作正在后台进行");
                    }

                    @Override
                    public void dotwo() {
                        ShareUtils.shareFriends(sharefile.getPath(), 1);
                        DialogUtil.dissDialog(dialog);
                        ToastUtil.makeShortText(mContext, "分享操作正在后台进行");
                    }

                    @Override
                    public void dothree() {

                    }
                }, sharefile);
                DialogUtil.showDialog(dialog);
            }
        }, 1000);
    }
    @Override
    public void onDestroy() {
        recycle(bitmap);
        super.onDestroy();
    }
}
